
package jfireeagle;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class Util
{
	
	static public Map<String, String> toMap(LocationParameters params)
	{

		Map<String, String> m = new LinkedHashMap<String, String>();
		
		if (params.getCellTower() != null)
		{
			m.putAll(Util.toMap(params.getCellTower()));
		}
		
		if (params.getAddress() != null)
		{
			m.putAll(Util.toMap(params.getAddress()));
		}
		
		if (params.getGpsCoordinate() != null)
		{
			m.putAll(Util.toMap(params.getGpsCoordinate()));
		}
		
		if (params.getWhereOnEarthId() != null)
		{
			m.putAll(Util.toMap(params.getWhereOnEarthId()));
		}
		
		if (params.getFreeText() != null)
		{
			m.put("address", params.getFreeText());
		}
			
		return m;
	}

	static public Map<String, String> toMap(WhereOnEarthId woeid)
	{
		
		Map<String, String> m = new LinkedHashMap<String, String>();
		m.put("woeid", woeid.getValue());
		
		return m;
	}
	
	static public Map<String, String> toMap(CellTower tower)
	{
		
		Map<String, String> m = new LinkedHashMap<String, String>();
		m.put("mcc", "" + tower.getMcc());
		m.put("mnc", "" + tower.getMnc());
		m.put("lac", "" + tower.getLac());
		m.put("cellid", "" + tower.getCellid());
		
		return m;
	}
	

	
	static public Map<String, String> toMap(GpsCoordinate coordinate)
	{
		Map<String, String> m = new LinkedHashMap<String, String>();
		m.put("lat", coordinate.getLatitude().toPlainString());
		m.put("lon", coordinate.getLongitude().toPlainString());
		
		return m;
	}
	
	static public Map<String, String> toMap(Address address)
	{
		Map<String, String> params = new LinkedHashMap<String, String>();
		params.put("address", address.getStreetAddress());
		params.put("city", address.getCity());
		params.put("state", address.getState());
		params.put("postal", address.getPostalCode());
		params.put("country", address.getCountry());
		
		return params;
	}

	static public void printTime(long time)
	{
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(time);
		
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		System.out.println(fmt.format(c.getTime()));
		
		
	}
	static public ClientSettings loadSettings(String file)
	{
		return loadSettings(new File(file));
	}
	
	static public ClientSettings loadSettings(File f)
	{
		if (f.exists() == false)
		{
			return new ClientSettings();
		}
		
		Properties props = new Properties();
		
		FileInputStream fis = null;
		
		try
		{
			fis = new FileInputStream(f);
			
			ClientSettings settings = loadSettings(fis);
			
			fis.close();
			
			return settings;
		}
		catch (IOException ex)
		{
			throw new RuntimeException(ex);
		}
		
		
		
	}
	
	static public ClientSettings loadSettings(InputStream input)
	{
		Properties props = new Properties();
		
		try
		{
			props.load(input);
			
			return loadSettings(props);
			
		}
		catch (IOException ex)
		{
			throw new RuntimeException(ex);
		}
	}
	
	static public ClientSettings loadSettings(Properties props)
	{
		ClientSettings settings = new ClientSettings();

		Token user = new Token();
		user.setPublicKey(props.getProperty("user.token"));
		user.setSecret(props.getProperty("user.secret"));
		
		if (user.isValid())
		{
			settings.setUserSpecificToken(user);
		}
		
		Token general = new Token();
		general.setPublicKey(props.getProperty("general.token"));
		general.setSecret(props.getProperty("general.secret"));

		settings.setGeneralToken(general);

		Token consumer = new Token();
		consumer.setPublicKey(props.getProperty("consumer.key"));
		consumer.setSecret(props.getProperty("consumer.secret"));

		settings.setConsumerToken(consumer);
		
		return settings;
	}

	static public File saveSettings(ClientSettings settings, String filename)
	{
		File f = new File(filename);
		
		return saveSettings(settings, f);
	}
	
	static public File saveSettings(ClientSettings settings, File f)
	{
		Properties props = new Properties();
		
		props.setProperty("user.token", settings.getUserSpecificToken().getPublicKey());
		props.setProperty("user.secret", settings.getUserSpecificToken().getSecret());

		props.setProperty("general.token", settings.getGeneralToken().getPublicKey());
		props.setProperty("general.secret", settings.getGeneralToken().getSecret());

		props.setProperty("consumer.key", settings.getConsumerToken().getPublicKey());
		props.setProperty("consumer.secret", settings.getConsumerToken().getSecret());
	
	
		try
		{
			FileOutputStream fos = new FileOutputStream(f);
			props.store(fos, "");
			
			fos.close();
		}
		catch (IOException ex)
		{
			throw new RuntimeException(ex);
		}
		
		return f;
		
	}
	
}
